package com.caine.androidtask.ls35;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class MyToast {
	Toast toast;
	Field field;
	Object obj;

	public MyToast(Context context, String msg) {
		Log.e("CaineZhu", "MyToast() jin lai mei you?" );
		try {
			Log.e("CaineZhu", "MyToast() 1" );
			toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
			Log.e("CaineZhu", "MyToast() 2" );
			toast.setGravity(Gravity.TOP
					| Gravity.CENTER_HORIZONTAL, 0, 0);
			Log.e("CaineZhu", "MyToast() 3" );
			field = toast.getClass().getDeclaredField("mTN");
			Log.e("CaineZhu", "MyToast() 4" );
			field.setAccessible(true);
			obj = field.get(toast);
			Log.e("CaineZhu", "MyToast() 5" );
			Log.e("CaineZhu", "MyToast() : "+"field : "+(field == null?"null":1)+" obj : "+(obj==null?" null":1));	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void showMyToast() {

		try {
			Log.e("CaineZhu", "MyToast() 6" );
			Method method = obj.getClass().getDeclaredMethod("show", null);
			method.invoke(obj, null);
			Log.e("CaineZhu", "showMyToast()");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void closeMyToast() {

		Method method;
		try {
			method = obj.getClass().getDeclaredMethod("hide", null);
			method.invoke(obj, null);
			Log.e("CaineZhu", "showMyToast()");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}